
#从Django中导入models模块
from django.db import models
# Create your models here.
# 此类继承于models下的Model
class Student(models.Model):
    gender_choices = (('男', '男'), ('女', '女'))
    sno = models.IntegerField(db_column="SNo", primary_key=True,null=False)
    name = models.CharField(db_column="SName", max_length=100, null=False)
    gender = models.CharField(db_column="Gender", max_length=100, choices=gender_choices, null=False)
    birthday = models.DateField(db_column="Birthday", null=False)
    mobile = models.CharField(db_column="Mobile", max_length=100)
    email = models.CharField(db_column="Email", max_length=100)
    address = models.CharField(db_column="Address", max_length=200)
    classname = models.CharField(db_column="classname", max_length=200, default="未分类")#classname字段我是在迁移完成后直接在数据库中添加的
    image = models.CharField(db_column="Image", max_length=200, null=False)
    def __str__(self):
        return "学号:%s\t姓名:%s\t性别:%s" % (self.sno, self.name, self.gender)
    #视图内置类
    class Meta:
        #managed 属性默认为 True，
        # 表示Django会负责创建、维护和更新数据库中的这张表。
        # 如果想手动控制这张表（例如，不让Django自动创建或迁移），可以将 managed 设置为 False。
        managed = True
            #指定数据库表名 (db_table)：默认情况下，Django会根据应用名和模型名自动生成数据库表的名称，
            # 但是可以通过设置 db_table 属性来指定模型对应的实际数据库表名。
            # 例如，db_table = "Student" 表示该模型将映射到数据库中名为 Student 的表。
        db_table = "Student"
        app_label = 'rootone'
class User(models.Model):
    rroot = models.OneToOneField('rootone.Rootone_if', on_delete=models.CASCADE)
    password = models.CharField(max_length=100, null=False, verbose_name='密码')


class Rootone_if(models.Model):
    root_id = models.CharField(verbose_name='管理员id', null=False, max_length=200, primary_key=True)
    username = models.CharField(db_column='Username', null=False, max_length=200)
    really_name = models.CharField(db_column='R_name', null=False, max_length=200)
    SEX_CHOOSE=[
        ('男', '男'),
        ('女', '女')
    ]
    sex = models.CharField(db_column='Sex', default='男', choices=SEX_CHOOSE, max_length=3)
    level = models.CharField(db_column='Level', max_length=200, null=False)
    email = models.CharField(db_column='Email', max_length=200, null=False)
    phone = models.CharField(db_column='Phone', null=False, max_length=200)
    password = models.CharField(db_column='Password', null=False, max_length=200)
    RANK = [
        (1, '学生'),
        (2, '教师'),
        (3, '工作人员'),
        (4, '超级管理员')
    ]
    rank = models.IntegerField(verbose_name='身份级别', choices=RANK, null=False)  # 职称


class Class(models.Model):
    class_id = models.CharField(verbose_name='班级编号，是班级的唯一标识码',
                                db_column='Class_id',
                                max_length=200,
                                primary_key=True,
                                null=False,
                                unique=True)

    class_name = models.CharField(verbose_name='班级名称', null=False, max_length=200, unique=True)
    class_sum = models.PositiveIntegerField(verbose_name='班级人数', null=False)
    CLASS_TYPE = [
        (1, "标准班"),
        (2, '国际班'),
        (3, '艺体特长班'),
        (4, '师范班')
    ]
    class_type = models.CharField(verbose_name='班级类型', choices=CLASS_TYPE, null=False, max_length=200)
    class_year = models.DateField(verbose_name='班级入学年份', null=False)
    notes = models.CharField(verbose_name='备注', null=True, max_length=200)
    created_at = models.DateTimeField(verbose_name='创建时间', null=True)
    updated_at = models.DateTimeField(verbose_name='最后的修改时间', null=True)
    exe_person = models.CharField(verbose_name='执行人', null=False, max_length=20)
    bzr = models.ForeignKey('teacherapp.Teacher', verbose_name='班主任', related_name='classes_as_bzr', on_delete=models.SET_NULL, null=True)
    fdy = models.ForeignKey('teacherapp.Teacher', verbose_name='辅导员', related_name='classes_as_fdy', on_delete=models.SET_NULL, null=True)
    root = models.ForeignKey('rootone.Rootone_if', on_delete=models.PROTECT)
    FAKE_DE=[
        (1, '显示'),
        (2, '回收站')
    ]
    fake = models.IntegerField(verbose_name='假删除的字段元素', choices=FAKE_DE, default=1)

# 统一设置辅导员和班级的管理合集,一个班级只能一个辅导员，但一个辅导员可以管理多个班级
class Other_teacher_class(models.Model):
    teacher = models.ForeignKey('teacherapp.Teacher', on_delete=models.SET_NULL, verbose_name='辅导员', null=True)
    cclass = models.ForeignKey('rootone.Class', on_delete=models.SET_NULL, verbose_name='班级', null=True)

    start_date = models.DateField(verbose_name='开始时间', null=True, blank=True)
    end_date = models.DateField(verbose_name='结束时间', null=True, blank=True)
    role = models.CharField(max_length=100, verbose_name='角色', help_text="如助教、任课教师等", null=True, blank=True)
    notes = models.TextField(verbose_name='备注', null=True, blank=True)


class Course(models.Model):
    course_id = models.CharField(max_length=200, primary_key=True, verbose_name='课程编号')
    name = models.CharField(max_length=200, null=False, blank=False, verbose_name='课程名称')
    description = models.TextField(null=True, blank=True, verbose_name='课程描述')
    credits = models.DecimalField(max_digits=5, decimal_places=2, null=False, verbose_name='学分')
    academic_year = models.CharField(max_length=10, null=True, blank=True, verbose_name='学年')

    location = models.CharField(max_length=200, null=True, blank=True, verbose_name='上课地点')
    STATUS_CHOICES = [
        ('planned', '计划中'),
        ('ongoing', '进行中'),
        ('completed', '已完成'),
    ]
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='planned', verbose_name='状态')
    notes = models.TextField(null=True, blank=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='最后修改时间')
    FAKE_DE = [
        (1, '显示'),
        (2, '回收站')
    ]

    # 新增外键字段，允许为空并设置默认值为 1
    root = models.ForeignKey(
        'rootone.Rootone_if',
        on_delete=models.PROTECT,
        null=True,  # 允许 NULL 值
        verbose_name='学院管理员的id'
    )

    # 新增整数字段，使用 choices 参数限制可选值，并设置默认值为 1
    fake = models.IntegerField(
        verbose_name='假删除的字段元素',
        choices=FAKE_DE,
        default=1,  # 默认值为 '显示'
        null=True,  # 允许 NULL 值
    )
    exe_person = models.CharField(verbose_name='执行人', null=True, max_length=20)


class CourseClassZj(models.Model):
    course = models.ForeignKey('rootone.Course', on_delete=models.SET_NULL, verbose_name='课程', null=True)
    cclass = models.ForeignKey('rootone.Class', on_delete=models.SET_NULL, verbose_name='班级', null=True)
    teacher = models.ForeignKey('teacherapp.Teacher', on_delete=models.SET_NULL, null=True, verbose_name='教师')
    DAY_CHOICES = [
        ('1', '星期一'),
        ('2', '星期二'),
        ('3', '星期三'),
        ('4', '星期四'),
        ('5', '星期五'),
        ('6', '星期六'),
        ('7', '星期日'),
    ]
    day_of_week = models.CharField(max_length=1, choices=DAY_CHOICES, verbose_name='星期几', null=True)

    PERIOD_CHOICES = [
        ("1", '第一节'),
        ("2", '第二节'),
        ("3", '第三节'),
        ("4", '第四节'),
        ("5", '第五节'),
        ("6", '第六节'),
        ("7", '第七节'),
        ("8", '第八节'),
        ("9", '第九节'),
        ("10", '第十节'),
    ]
    period = models.CharField(max_length=2, choices=PERIOD_CHOICES, verbose_name='节次', null=True)
    location = models.ForeignKey('rootone.Location', on_delete=models.CASCADE, verbose_name='上课地点', null=True)
    notes = models.TextField(null=True, blank=True, verbose_name='备注')

    class Meta:
        verbose_name = '课程班级课表'
        verbose_name_plural = '课程班级课表'
        unique_together = ['course', 'cclass', 'day_of_week', 'period']  # 确保同一班级在同一时间只有一个课程安排
        constraints = [
            models.UniqueConstraint(
                fields=['teacher', 'day_of_week', 'period'],  # 确保同一时间只有一个教师，即保证同一时间段的教师唯一性
                name='unique_teacher_time_slot'
            )]

#     增加时间单位来保证课程地点唯一性
    update_tiem = models.DateTimeField(verbose_name='更新时间', null=True)


class CourseTeacherZj(models.Model):
    course = models.ForeignKey('rootone.Course', on_delete=models.SET_NULL, null=True, verbose_name='课程')
    teacher = models.ForeignKey('teacherapp.Teacher', on_delete=models.SET_NULL, null=True, verbose_name='教师')
    role = models.CharField(max_length=100, null=True, blank=True, verbose_name='角色', help_text="如讲师、助教等")
    notes = models.TextField(null=True, blank=True, verbose_name='备注')

# 上课地点模型, 考试安排也可使用
class Location(models.Model):
    location_id = models.CharField(max_length=200, primary_key=True, verbose_name='地点编号')
    name = models.CharField(max_length=200, null=False, blank=False, verbose_name='地点名称')
    address = models.TextField(null=True, blank=True, verbose_name='详细地址')
    capacity = models.PositiveIntegerField(null=True, blank=True, verbose_name='容量')
    notes = models.TextField(null=True, blank=True, verbose_name='备注')

# 课程和地址的中间表
class CourseLocationZj(models.Model):
    course = models.ForeignKey('rootone.Course', on_delete=models.SET_NULL, null=True, verbose_name='课程')
    location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True, verbose_name='上课地点')
    notes = models.TextField(null=True, blank=True, verbose_name='备注')
    sections = models.CharField(null=True, verbose_name='节次，依此来标识', max_length=200)


#考试安排和地址的中间表
class ExaLocationZj(models.Model):
    course = models.ForeignKey('rootone.Course', on_delete=models.SET_NULL, null=True, verbose_name='课程')
    location = models.ForeignKey(Location, on_delete=models.SET_NULL, null=True, verbose_name='考试地点')
    start_at = models.DateTimeField(auto_now_add=True, verbose_name='开始时间', null=True)
    end_at = models.DateTimeField(auto_now=True, verbose_name='结束时间', null=True)
    # 监考人
    teacher = models.ForeignKey('teacherapp.Teacher', verbose_name='监考人', null=True, on_delete=models.SET_NULL)
    cclass = models.ForeignKey('rootone.Class', verbose_name='班级', null=True, on_delete=models.SET_NULL)